//
//  YLCFillInPersonMessageCell.m
//  LinYa
//
//  Created by 初程程 on 2018/1/30.
//  Copyright © 2018年 初程程. All rights reserved.
//

#import "YLCFillInPersonMessageCell.h"

@implementation YLCFillInPersonMessageCell
{
    UILabel *titleLabel;
    UITextField *messageField;
    UILabel *unitLabel;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self createCustomView];
        
    }
    return self;
}
- (void)createCustomView{
    
    titleLabel = [[UILabel alloc] init];//[YLCFactory createLabelWithFont:16 color:RGB(119, 123, 124)];
    
//    titleLabel.textColor = RGB(119, 123, 124);
    
    [self.contentView addSubview:titleLabel];

    messageField = [[UITextField alloc] init];
    
    UIView *right = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 13*MarginFit, self.frameHeight)];

    messageField.rightView = right;
    
    messageField.rightViewMode = UITextFieldViewModeAlways;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    
    [right addGestureRecognizer:tap];
    
    [messageField.rac_textSignal subscribeNext:^(id x) {
        if (JK_IS_DELEGATE_RSP_SEL(self.delegate, didChangeFillInFieldWithText:index:)) {
            [self.delegate didChangeFillInFieldWithText:x index:_index];
        }
    }];
    
    [self.contentView addSubview:messageField];
    
    unitLabel = [[UILabel alloc] init];

    unitLabel.hidden = YES;
    
    [self.contentView addSubview:unitLabel];
    
    [self createFrame];
}

- (void)tapClick{
    [messageField becomeFirstResponder];
}
- (void)setIsRight:(BOOL)isRight{
    if (isRight) {
        messageField.textAlignment = NSTextAlignmentRight;
    }else{
        messageField.textAlignment = NSTextAlignmentLeft;
    }
}
- (void)setIsNumber:(BOOL)isNumber{
    if (isNumber) {
        messageField.keyboardType = UIKeyboardTypeNumberPad;
    }else{
        messageField.keyboardType = UIKeyboardTypeDefault;
    }
}
- (void)setIsUnit:(BOOL)isUnit{
    WS(weakSelf)
    if (isUnit) {
        unitLabel.hidden = NO;
    }else{
        unitLabel.hidden = YES;
        [messageField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.mas_right).offset(0);
            make.left.equalTo(titleLabel.mas_right).offset(10*MarginFit);
            make.centerY.equalTo(weakSelf.mas_centerY);
            make.height.equalTo(weakSelf.frameHeight);
        }];
    }
}
- (void)setText:(NSString *)text{
    messageField.text = [text notNullString];
}
- (void)setUnitStr:(NSString *)unitStr{
    unitLabel.text = [unitStr notNullString];
}
- (void)setTitle:(NSString *)title{
    titleLabel.text = [title notNullString];
}
- (void)setPlaceholder:(NSString *)placeholder{
    messageField.placeholder = placeholder;
}
- (void)createFrame{
    WS(weakSelf)
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(13*MarginFit);;
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.width.equalTo(90);
    }];
    
    [unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.centerY.equalTo(weakSelf.mas_centerY);
    }];
    
    [messageField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(unitLabel.mas_left).offset(0);
        make.left.equalTo(titleLabel.mas_right).offset(10*MarginFit);
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.height.equalTo(weakSelf.frameHeight);
    }];
}
- (void)layoutSubviews{
    [super layoutSubviews];
}
@end
